<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 设备分组
 *
 * @author      Eden
 * @datetime    2016/12/7 17:56
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Group extends ADM_Controller
{
    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->view(parent::TMP_DIR . '/stream/group.html');
    }

    /**
     * 获取分组列表
     */
    public function getGroupLists()
    {
        $this->load->model('admin/stream/Group_Model', 'stream');
        $res = $this->stream->getLists();
        $this->output->send(array(
            'state' => true,
            'data' => !empty($res) ? $res : array()
        ));
    }

    /**
     * 添加分组
     */
    public function addGroup()
    {
        $send['state'] = false;
        do {

            $groupName = $this->input->post('groupName');
            if (empty($groupName)) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('admin/stream/Group_Model', 'stream');
            $res = $this->stream->addGroup($groupName);
            if (!$res) {
                $err = $this->stream->getError();
                $send['error'] = !empty($err) ? $err : '添加失败';
                break;
            }
            $send['state'] = true;
            $send['data'] = $res;
        } while(false);
        $this->output->send($send);
    }

    /**
     * 删除
     */
    public function deleteGroup()
    {
        $send['state'] = false;
        do {

            $groupId = $this->input->get('groupId');
            if (empty($groupId)) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('admin/stream/Group_Model', 'stream');
            $res = $this->stream->deleteGroup($groupId);
            if (!$res) {
                $err = $this->stream->getError();
                $send['error'] = !empty($err) ? $err : '删除失败';
                break;
            }
            $send['state'] = true;
            $send['data'] = $res;
        } while(false);
        $this->output->send($send);
    }

    /*
     * 编辑
     */
    public function editGroup()
    {
        $send['state'] = false;
        do {

            $groupName = $this->input->post('groupName');
            $groupId = $this->input->post('groupId');
            if (empty($groupName) || empty($groupId)) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('admin/stream/Group_Model', 'stream');
            $res = $this->stream->editGroup($groupId, $groupName);
            if (!$res) {
                $err = $this->stream->getError();
                $send['error'] = !empty($err) ? $err : '添加失败';
                break;
            }
            $send['state'] = true;
            $send['data'] = $res;
        } while(false);
        $this->output->send($send);
    }
}
